import cv2
cv2.namedWindow('Output')
#实例化相机
camdevice = cv2.VideoCapture(0)
#设置相机分辨率
camdevice.set(cv2.CAP_PROP_FRAME_WIDTH,640)
camdevice.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
face_cascade = cv2.CascadeClassifier(r"C:\Users\56526\AppData\Local\Programs\Python\Python313\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml")
#循环采集
while True:
    ret,img = camdevice.read()#采集图片
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#彩色图片转灰度图
    faces = face_cascade.detectMultiScale(gray,1.1,4)#检测人脸
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
    cv2.imshow("Output",img)
    k = cv2.waitKey(5)  & 0xff
    if k == 27:
        break
#销毁相机
camdevice.release()
